package com.example.springboot.controller;


import com.example.springboot.pojo.Equipment;
import com.example.springboot.pojo.Workshop;
import com.example.springboot.service.EquipmentServiceImpl;
import com.example.springboot.service.WorkshopServiceImpl;
import com.example.springboot.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Workshop")
public class WorkshopController {
    @Autowired
    @Qualifier("workshopService")
    WorkshopServiceImpl workshopService;
    @Autowired
    @Qualifier("equipmentService")
    EquipmentServiceImpl equipmentService;
    @RequestMapping("/addWorkshop")
    public String addWorkshop(Model model, Workshop workshop,HttpSession session){
        System.out.println("=============");
        boolean flag = workshopService.addWorkshop(workshop);
        model.addAttribute("flag",flag);
        return "redirect:/Workshop/getWorkshopList/"+session.getAttribute("currentPage");
    }

    @RequestMapping("/getWorkshopList/{pageNum}")
    public String getWorkshopList(Model model, @PathVariable int pageNum, HttpSession session){
        List<Workshop> workshopList = workshopService.getWorkshopList();
        Page<Workshop> page = new Page<>(pageNum,8,workshopList);
        model.addAttribute("page",page);
        //System.out.println(page.dataList);
        session.setAttribute("currentPage",page.currentPage);
        //System.out.println(workshopList);
        return "workshopList";
    }

    @RequestMapping("/queryById/{id}")
    public void queryById(Model model,@PathVariable int id){
        Workshop workshop = workshopService.queryById(id);
        model.addAttribute("workshop",workshop);
    }

    @RequestMapping("/deleteById/{id}")
    public String deleteById(Model model,@PathVariable int id,HttpSession session){
        boolean flag = workshopService.deleteById(id);
        model.addAttribute("flag",flag);
        return "redirect:/Workshop/getWorkshopList/"+session.getAttribute("currentPage");
    }

    @RequestMapping("/updateById/{id}")
    public String updateById(Model model,Workshop workshop,@PathVariable int id,HttpSession session){
        //System.out.println("workshopname="+workshop.getName());
        workshop.setId(id);
        boolean flag = workshopService.updateById(workshop);
        model.addAttribute("flag",flag);
        return "redirect:/Workshop/getWorkshopList/"+session.getAttribute("currentPage");
    }

    @RequestMapping("/")
    public String showWorkshop(Model model,HttpSession session){
        List<Workshop> workshopList = workshopService.getWorkshopList();
        Page<Workshop> page = new Page<>(0,8,workshopList);
        model.addAttribute("page",page.dataList);
        session.setAttribute("currentPage",page.currentPage);
        return "index";
    }

    @RequestMapping("/showAddWorkshop")
    public String showAddWorkshop(Model model){
        return "addWorkshop";
    }

    @RequestMapping("/getWorkshopListByCondition/{pageNum}")
    public String getWorkshopListByCondition(@PathVariable int pageNum,Model model,String name,String manager,HttpSession session){
        Map<String,String> map=new HashMap<>();
        System.out.println("name="+name);
        if(name.isEmpty())
        map.put("name",null);
        else
        map.put("name",name);
        if(manager.isEmpty())
            map.put("manager",null);
        else
            map.put("manager",manager);
        session.setAttribute("wsmap",map);
        List<Workshop> workshopList = workshopService.getWorkshopListByCondition(map);
        System.out.println(workshopList);
        Page<Workshop> page = new Page<>(pageNum,8,workshopList);
        model.addAttribute("page",page);
        session.setAttribute("currentPage",page.currentPage);
        return "workshopListByCon";
    }

    @RequestMapping("/getWorkshopListByCondition2/{pageNum}")
    public String getWorkshopListByCondition2(@PathVariable int pageNum,Model model,HttpSession session){
        System.out.println("============");
        Map<String,String> map= (Map<String, String>) session.getAttribute("wsmap");
        List<Workshop> workshopList = workshopService.getWorkshopListByCondition(map);
        Page<Workshop> page = new Page<>(pageNum,8,workshopList);
        model.addAttribute("page",page);
        session.setAttribute("currentPage",page.currentPage);
        return "workshopListByCon";
    }

    @RequestMapping("/getEquipmentList/{pageNum}/{id}")
    public String getEquipmentList(Model model,@PathVariable int id,@PathVariable int pageNum,HttpSession session){
        List<Equipment> equipmentList = workshopService.getEquipmentListById(id);
        System.out.println(equipmentList);
        Page<Equipment> page = new Page<>(pageNum,8,equipmentList);
        model.addAttribute("page",page);
        session.setAttribute("currentPage",page.currentPage);
        session.setAttribute("wsid",id);
        return "equipmentListById";
    }
    @RequestMapping("/getEquipmentList/{pageNum}")
    public String getEquipmentList2(Model model,@PathVariable int pageNum,HttpSession session){
        int id= (int) session.getAttribute("wsid");
        List<Equipment> equipmentList = workshopService.getEquipmentListById(id);
        Page<Equipment> page = new Page<>(pageNum,8,equipmentList);
        model.addAttribute("page",page);
        session.setAttribute("currentPage",page.currentPage);
        session.setAttribute("wsid",id);
        return "equipmentListById";
    }

    @RequestMapping("/showUpdateWorkshop/{id}")
    public String showUpdateWorkshop(Model model,@PathVariable int id){
        Workshop workshop = workshopService.queryById(id);
        model.addAttribute("workshop",workshop);
        return "updateWorkshop";
    }




}
